﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

using Visifire.Charts;
using sil;
using ESRI.ArcGIS.Client.Symbols;
using System.Threading;
using System.Windows.Threading;

using System.Collections.ObjectModel;

using GisWeb.ServiceReference1;
using GisWeb;
using GisWeb.参数配置窗体;



namespace GisWeb
{
    public partial class MainPage : UserControl
    {
        List<item> items = new List<item>();
        String chooseIndu = "";
        String chooseCareer = "";
        private void btnAbout2_Click(object sender, RoutedEventArgs e)
        {
            switch (mapclick)
            {
                case 0:
                    investBase.Show();
                    investBase.showFlag = 1;
                    investBase.stopAna += investBase_Closed;
                    investBase.checkResult += new EventHandler(investBase_checkResult);
                    mapclick = 4;
                    break;
                case 1: MessageBox.Show("正在运行基础查询模块，请先停止再进行查询"); break;
                case 2: MessageBox.Show("正在运行对比分析模块，请先停止再进行查询"); break;
                case 3: MessageBox.Show("正在运行经济指数预测模块，请先停止再进行查询"); break;
                case 4: MessageBox.Show("正在运行投资分析模块，请先停止再进行查询"); break;
                case 5: MessageBox.Show("正在运行重心转移模块，请先停止再进行查询"); break;

            }

        }

        void investBase_checkResult(object sender, EventArgs e)
        {
            Query query =new Query();
                query.Where="1=1";
                query.ReturnGeometry = false;
                QueryTask qt = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/3");
            qt.ExecuteCompleted+=new EventHandler<QueryEventArgs>(qt_ExecuteCompleted1);
            qt.ExecuteAsync(query);
        }

        void qt_ExecuteCompleted1(object sender, QueryEventArgs e) {
            FeatureSet set = e.FeatureSet;
           
            DateTime time = DateTime.Parse("2010-12-30");
            int indu= investBase.indu.SelectedIndex;
            items.Clear();
           
            chooseIndu = indu == 0 ? "INDUSTRY" : "AGRICULTURAL";
            ServiceReference1.GISServiceClient client = new GISServiceClient();
            client.GetLQCompleted += new EventHandler<GetLQCompletedEventArgs>(client_GetLQCompleted);
            int career = investBase.career.SelectedIndex;
            if (indu == 0) {
                switch (career) {
                    case 0: chooseCareer = "INDUSTRYVALUE"; break;
                    case 1: chooseCareer = "FIANCEVALUE"; break;
                    case 2: chooseCareer = "BUILDINGVALUE"; break;
                    case 3: chooseCareer = "TRANSPORTVALUE"; break;
                    case 4: chooseCareer = "RETAILVALUE"; break;
                    case 5: chooseCareer = "SERVICEVALUE"; break;
                    case 6: chooseCareer = "TOURISMVALUE"; break;  
                }
            
            
            

            }
            else if (indu == 1) {
                switch (career)
                {
                    case 0: chooseCareer = "FARMINGVALUE"; break;
                    case 1: chooseCareer = "FORESTYVALUE"; break;
                    case 2: chooseCareer = "FISHERYVALUE"; break;
                    case 3: chooseCareer = "HUSBANDRYVALUE"; break;
                }
            
            
            
            
            }
            if (set.Count() < 1)
            {
                MessageBox.Show("找不到市");

            }
            else {
                foreach (Graphic graph in set) {
                  
                    client.GetLQAsync(graph.Attributes["NAME"].ToString(), "", time, chooseIndu, chooseCareer, graph.Attributes["NAME"].ToString());
                
                
                }
                new Thread(() =>
                {
                    int flag = 1;
                    while (flag == 1)
                    {

                        if (items.Count < 14)
                        {
                            handle.WaitOne(100);

                        }
                        else { flag = 0; }
                    }
                    List<item> newItem = new List<item>();
                    foreach (item a in items.OrderByDescending(item => item.result))
                    {
                        newItem.Add(a);
                       

                    }

                    this.Dispatcher.BeginInvoke(() =>
                    {
                        investWindow iwin = new investWindow();
                        iwin.ParentLayoutRoot = resultFolder;
                        iwin.Title = investBase.career.SelectedValue;
                        iwin.Show();
                        iwin.test.Text = chooseIndu + "     " + chooseCareer;
                        iwin.testGrid.ItemsSource = newItem;

                        for (int ttt = 0; ttt < 3; ttt++)
                        {
                            Query query = new Query();
                            query.Text = newItem[ttt].name.Substring(0,newItem[ttt].name.Length-1);
                            query.ReturnGeometry = true;
                            QueryTask qt = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/1");
                            qt.ExecuteCompleted += new EventHandler<QueryEventArgs>(qt_ExecuteCompleted2);
                            qt.ExecuteAsync(query,newItem[ttt].result);
                            MessageBox.Show("ttt");

                        }

                    });
                   



                }).Start();
            }
        
        
        }
        void qt_ExecuteCompleted2(object sender, QueryEventArgs e) { 
        foreach(Graphic t in e.FeatureSet){
            t.Attributes.Add("focus",e.UserState);
            //t.Symbol = LayoutRoot.Resources["mark"] as SimpleMarkerSymbol;
            //        gra.Graphics.Add(t);
            Query query = new Query();
            query.Text = t.Attributes["NAME"].ToString();
            query.Where = "TIME like '%2010%'";
            query.ReturnGeometry = true;
            QueryTask qt = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/17");
            qt.ExecuteCompleted += (object send, QueryEventArgs ee) => { t.Attributes.Add("intense", ee.FeatureSet.Features[0].Attributes[chooseCareer.Substring(0, chooseCareer.Length - 5) + "INDEX"]); MessageBox.Show("over"); };
            qt.ExecuteAsync(query);
            Query query1 = new Query();
            query1.Text = t.Attributes["NAME"].ToString();
            query1.Where = "TIME like '%2010%'";
            query1.ReturnGeometry = true;
            QueryTask qt1 = new QueryTask("http://arcgis:6080/arcgis/rest/services/HuNanMapService/MapServer/18");
            qt1.ExecuteCompleted += (object send, QueryEventArgs ee) => { t.Attributes.Add("confidence", ee.FeatureSet.Features[0].Attributes[chooseCareer.Substring(0, chooseCareer.Length - 5) + "INDEX"]); MessageBox.Show("over"); };
            qt1.ExecuteAsync(query1);
            t.Attributes.Add("range", 1);
            new Thread(() =>
            {
                while (qt.IsBusy || qt1.IsBusy)
                {
                    handle.WaitOne(100);



                }

                this.Dispatcher.BeginInvoke(() =>
                {
                    t.Symbol = LayoutRoot.Resources["Marker1"] as SimpleMarkerSymbol;
                    gra.Graphics.Add(t);
                    MessageBox.Show(t.Attributes["range"].ToString()+t.Attributes["confidence"]);

                });


            }).Start();
        }
        
        
        
        }

        void client_GetLQCompleted(object sender, GetLQCompletedEventArgs e)
        {
            item citys = new item();
            citys.result =double.Parse( e.Result.ToString());
            citys.name = e.UserState.ToString();
            items.Add(citys);

          
           
            
        }

        

        void investBase_Closed(object sender, EventArgs e)
        {
            investBase.Close();
            investBase.showFlag = 0;
            mapclick = 0;
        }

    
    }
    public class item {
        public double result
        {
            get;
            set;
        }

        public String name
        {
            get;
            set;
        }
    
    } 
}